﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class MvcControllerClassService : IMvcControllerClassService
    {
        Domain.Uow.IRepository<Domain.Entity.MvcControllerClass> controllerClassRepository;

        public MvcControllerClassService(Domain.Uow.IRepository<Domain.Entity.MvcControllerClass> controllerClass)
        {
            if (controllerClass == null)
            {
                throw new ArgumentNullException("controllerClassService is null");
            }
            controllerClassRepository = controllerClass;
        }

        public Domain.Entity.MvcControllerClass Add(Domain.Entity.MvcControllerClass type, Domain.Entity.User operUser)
        {
            try
            {
                controllerClassRepository.Add(type);
                controllerClassRepository.Uow.Commit();
            }
            catch (Exception ex)
            {
            }
            return type;
        }

        public bool Delete(Domain.Entity.MvcControllerClass type, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                type.State = false;
                controllerClassRepository.Modify(type);
                controllerClassRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.MvcControllerClass type, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                controllerClassRepository.Delete(type);
                controllerClassRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = controllerClassRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                item.State = false;
                controllerClassRepository.Modify(item);
            }
            try
            {
                controllerClassRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var res = false;
            var delete = controllerClassRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                controllerClassRepository.Delete(item);
            }
            try
            {
                controllerClassRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.MvcControllerClass type, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                controllerClassRepository.Modify(type);
                controllerClassRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.MvcControllerClass Get(int id)
        {
            var data = controllerClassRepository.Get(id);
            return data;
        }

        public IQueryable<Domain.Entity.MvcControllerClass> GetList()
        {
            var res = controllerClassRepository.GetList().AsQueryable();
            return res;
        }
    }
}
